// app.js
const moment = require('./utils/moment')

App({
  hidetabbar () {
    wx.hideTabBar({
      fail () {
        setTimeout(() => {
          wx.hideTabBar()
        }, 200)
      }
    })
  },
  getSystemInfo () {
    let that = this
    wx.getSystemInfo({
      success (res) {
        that.globalData.systemInfo = res
      }
    })
  },
  editTabbar () {
    this.hidetabbar()
    let tabbar = this.globalData.tabBar
    let currentPages = getCurrentPages()
    let t = currentPages[currentPages.length - 1]
    let pagePath = t.route;
    (pagePath.indexOf('/') !== 0) && (pagePath = '/' + pagePath)
    for (let i in tabbar.list) {
      tabbar.list[i].selected = false
      ;(tabbar.list[i].pagePath === pagePath) && (tabbar.list[i].selected = true)
    }
    t.setData({ tabbar })
  },
  onLaunch: function (options) {
    this.hidetabbar()
    this.getSystemInfo()
    if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力')
    } else {
      wx.cloud.init({
        // env 参数说明：
        //   env 参数决定接下来小程序发起的云开发调用（wx.cloud.xxx）会默认请求到哪个云环境的资源
        //   此处请填入环境 ID, 环境 ID 可打开云控制台查看
        //   如不填则使用默认环境（第一个创建的环境）
        // env: 'my-env-id',
        env: 'cloud1-0gsghqc8f133d137',
        traceUser: true
      })
    }
  },

  isDaka () {
    const thisMsg = wx.getStorageSync('loginMsg') && JSON.parse(wx.getStorageSync('loginMsg'))
    return thisMsg && thisMsg.signUp[thisMsg.signUp.length - 1] === parseInt(moment().format('YYYYMMDD'))
  },

  globalData: {
    popup: false,
    isLogin () {
      return !!wx.getStorageSync('loginMsg')
    },
    systemInfo: null,
    tabBar: {
      'color': '#000',
      'selectedColor': '#48bafb',
      'list': [
        {
          'pagePath': '/pages/location/location',
          'text': '打卡',
          'isDaka': true,
          'iconPath': 'icon/location.png',
          'selectedIconPath': 'icon/location-h.png'
        },
        {
          'pagePath': '/pages/index/index',
          'text': '首页',
          'isSpecial': true,
          'iconPath': 'icon/index.png',
          'selectedIconPath': 'icon/index-h.png'
        },
        {
          'pagePath': '/pages/home/home',
          'text': '我',
          'iconPath': 'icon/home.png',
          'selectedIconPath': 'icon/home-h.png'
        }
      ]
    }
  }

})
